'use strict';
var cmsControllers = angular.module('cmsControllers');

cmsControllers
    .controller('CmsStoreCtrl', ['$rootScope', '$scope', '$routeParams', 'StringUtil', 'uploadService',
            'StoreOpt', 'Category', 'ProductOpt', 'Product',
    function ($rootScope, $scope, $routeParams, StringUtil, uploadService, StoreOpt, Category, ProductOpt, Product) {
        $scope.showCategoryList = function() {
            $scope.storeContent = '/ext/components/store/cms/view/category-list.html';
            var json = StoreOpt.opt({action: 'queryStoreBySubId', subId: $rootScope.CurrentSub.id},function() {
                $scope.store = json.data.store;
                $scope.categories = json.data.categories;
            });
        }
        $scope.showCategoryList();

        $scope.createCategory = function() {
            uploadService.maxNumb = 1;
            uploadService.files = [];
            $scope.storeContent = '/ext/components/store/cms/view/category-edit.html';
            $scope.category = {storeId: $scope.store.id};
        };
        $scope.editCategory = function(cate) {
            uploadService.maxNumb = 1;
            uploadService.files = [];
            $scope.storeContent = '/ext/components/store/cms/view/category-edit.html';
            $scope.category =cate;

            uploadService.files.push({url: cate.image, thumbnailUrl: uploadService.imageUrl(cate.image, 'thumbnail')});
            $scope.files = uploadService.files;
        };
        $scope.saveCategory = function() {
            $scope.category.image = uploadService.files[0].url
            new Category($scope.category).$save().then(function() {
                $scope.showCategoryList();
            });
        };
        $scope.deleteCategory = function(cate) {
            new Category(cate).$delete().then(function() {
                $scope.showCategoryList();
            });
        };
        $scope.addProducts = function(cate) {
            $scope.category = cate;
            $scope.storeContent = '/ext/components/store/cms/view/category-list.html';
            $rootScope.CurrentCategory = cate;
            $scope.showProductList();
        };

        $scope.showProductList = function() {
            $scope.storeContent = '/ext/components/store/cms/view/product-list.html';
            var json = ProductOpt.opt({action: 'queryProductByCategoryId', categoryId: $rootScope.CurrentCategory.id},function() {
                $scope.products = json.data.products;
            });
        }
        $scope.createProduct = function() {
            uploadService.maxNumb = 3;
            uploadService.files = [];
            $scope.storeContent = '/ext/components/store/cms/view/product-edit.html';
            $scope.product = {categoryId: $scope.category.id};
        };
        $scope.editProduct = function(product) {
            uploadService.maxNumb = 3;
            uploadService.files = [];
            $scope.storeContent = '/ext/components/store/cms/view/product-edit.html';
            $scope.product = product;

            angular.forEach(product.images, function(image, key){
                this.push({
                    url: image.url,
                    thumbnailUrl: uploadService.imageUrl(image.url, 'thumbnail')
                });
            }, uploadService.files);
            $scope.files = uploadService.files;
        };
        $scope.saveProduct = function() {
            $scope.product.images = uploadService.files
            new Product($scope.product).$save().then(function() {
                $scope.showProductList();
            });
        };
        $scope.deleteProduct = function(product) {
            new Product(product).$delete().then(function() {
                $scope.showProductList();
            });
        };

        $scope.noImage = function() {
            if (uploadService.files.length < 1) {
                return true;
            } else {
                return false;
            }
        };
    }]);






